// pages/order/order.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    catalogs: [{
      "catalogName": "全部",
      "select": 3},{
      "catalogName": "代付款",
      "select": 0},{
      "catalogName": "已完成",
      "select": 1},{
      "catalogName": "已取消",
      "select": 2},
    ],
    catalogSelect: 3,//判断是否选中
    store_name:'快餐店',//店铺名称

    orders:[],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //ajax参数为3查询所有
    wx.request({
      url: 'http://localhost:8080/fullboy/queryuserorder.action',
      data: {
        uid: app.globalData.uId,
        struts: 3
      },
      method: 'GET',
      success: function (res) {
        console.log(res);
        var order = res.data.data;
        var orderList = [];
        for(var k=0;k<order.length;k++){
          var orders = new Object();
          orders.orderId = order[k][0].orderId;
          orders.orderNo = order[k][0].orderNo;
          orders.uId = order[k][0].uId;
          orders.tableNo = order[k][0].tableNo;
          orders.pNum = order[k][0].pNum;
          orders.remark = order[k][0].remark;
          orders.createTime = order[k][0].createTime;
          orders.totalMoney = order[k][0].totalMoney;
          orders.payType = order[k][0].payType;
          orders.struts = order[k][0].struts;
          var orderDetail = [];
          if (order[k][2][0] == undefined){
            var details = new Object();
            details.oDId = order[k][1].oDId;
            details.orderId = order[k][1].orderId;
            var T_dishe = order[k][2];
            details.T_dishes = T_dishe;
            details.fCount = order[k][1].count;
            details.cooked = order[k][1].cooked;
            orderDetail.push(details);
          }else{
            for (var i = 0; i < order[k][2].length; i++) {
              var details = new Object();
              details.oDId = order[k][1].oDId;
              details.orderId = order[k][1].orderId;
              var T_dishe = order[k][2][i];
              details.T_dishes = T_dishe;
              details.fCount = order[k][1].count;
              details.cooked = order[k][1].cooked;
              orderDetail.push(details);
            }
          }
          
          orders.orderDetail = orderDetail;
          orders.sumCount = 0;
          orders.showView = true;
          orderList.push(orders);
        }
        that.setData({
          orders: orderList
        });
        console.log(that.data.orders);
        that.fSumCount(that);
      }
    })
  },

  /**
   * 去支付
   */
  pay_ok:function(e){
    var id = e.currentTarget.id
    var orderId = this.data.orders[id].orderId;
    var orderNo = this.data.orders[id].orderNo;
    var Money = this.data.orders[id].totalMoney;
    var createTime = this.data.orders[id].createTime;
    wx.navigateTo({
      url: '/pages/pay/pay?orderId=' + orderId + '&orderNo=' + orderNo + '&totalMoney=' + Money + '&createTime=' + createTime,
    })
  },

  /**
   * 更新每个订单的菜品总份数
   */
  fSumCount: function (that){
    var orders = that.data.orders;
    for(var i=0;i<orders.length;i++){
      var orderDetail = that.data.orders[i].orderDetail;
      var sum = 0;
      for (var j = 0; j < orderDetail.length; j++) {
        sum += orderDetail[j].fCount;
      }
      var order = "orders[" + i +"].sumCount";
      that.setData({
        [order]: sum
      });
    }
  },

  /**
   * 点击查看菜
   */
  onChangeShowState: function (e) {
    var id = e.currentTarget.id
    var that = this;
    var view = "orders[" + id + "].showView";
    that.setData({
      [view]: (!that.data.orders[id].showView)
    });
  },
  /**
   * 点击导航栏更新选中值
   */
  chooseCatalog: function (data) {
    console.log(data.currentTarget.dataset.select);
    var that = this;
    that.setData({//把选中值放入判断值
      catalogSelect: data.currentTarget.dataset.select
    })
    //ajax参数为选中值0->查询待付款，1->查询已付款，2->查询已取消，3->查询所有
    wx.request({
      url: 'http://localhost:8080/fullboy/queryuserorder.action',
      data: {
        uid: app.globalData.uId,
        struts: data.currentTarget.dataset.select
      },
      method: 'GET',
      success: function (res) {
        var order = res.data.data;
        var orderList = [];
        for (var k = 0; k < order.length; k++) {
          var orders = new Object();
          orders.orderId = order[k][0].orderId;
          orders.orderNo = order[k][0].orderNo;
          orders.uId = order[k][0].uId;
          orders.tableNo = order[k][0].tableNo;
          orders.pNum = order[k][0].pNum;
          orders.remark = order[k][0].remark;
          orders.createTime = order[k][0].createTime;
          orders.totalMoney = order[k][0].totalMoney;
          orders.payType = order[k][0].payType;
          orders.struts = order[k][0].struts;
          var orderDetail = [];
          if (order[k][2][0] == undefined) {
            var details = new Object();
            details.oDId = order[k][1].oDId;
            details.orderId = order[k][1].orderId;
            var T_dishe = order[k][2];
            details.T_dishes = T_dishe;
            details.fCount = order[k][1].count;
            details.cooked = order[k][1].cooked;
            orderDetail.push(details);
          } else {
            for (var i = 0; i < order[k][2].length; i++) {
              var details = new Object();
              details.oDId = order[k][1].oDId;
              details.orderId = order[k][1].orderId;
              var T_dishe = order[k][2][i];
              details.T_dishes = T_dishe;
              details.fCount = order[k][1].count;
              details.cooked = order[k][1].cooked;
              orderDetail.push(details);
            }
          }

          orders.orderDetail = orderDetail;
          orders.sumCount = 0;
          orders.showView = true;
          orderList.push(orders);
        }
        that.setData({
          orders: orderList
        });
        console.log(that.data.orders);
        that.fSumCount(that);
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})